package thebesttaskmanager.idmanager;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;

import org.junit.Test;

public class IdManagerTest {
  @Test
  public void testDeleteId() {
    ArrayList<Integer> ids = new ArrayList<Integer>();
    ids.add(0);
    ids.add(5);

    IdManager.setIdList(ids);
    assertFalse("Test failed ID delete", IdManager.deleteId(2));
    assertTrue("Test successful ID delete", IdManager.deleteId(5));
    assertFalse("Test failed ID delete", IdManager.deleteId(5));

  }

  @Test
  public void testGetNextId() {
    // test adding new ids

    IdManager.setIdList(new ArrayList<Integer>());
    assertEquals("Get 1st id", IdManager.getNextId(), 0);
    assertEquals("Get 2nd id", IdManager.getNextId(), 1);
    assertEquals("Get 3rd id", IdManager.getNextId(), 2);
    IdManager.deleteId(1);

    assertEquals("Get 4th id", IdManager.getNextId(), 1);

    // test id limit reached
    for (int i = 0; i < IdManager.MAX_IDS; i++) {
      IdManager.getNextId();
    }
    assertEquals("Test id limit reached", IdManager.getNextId(), -1);

  }

  @Test
  public void testAddNewId() {
    // test adding an id that already exists
    ArrayList<Integer> newIds = new ArrayList<Integer>();
    newIds.add(4);
    IdManager.setIdList(newIds);
    assertFalse("Test return false adding existing id", IdManager.addNewId(4));

    // test adding an id that does not exist
    assertTrue("Test return true successful adding of id", IdManager.addNewId(3));
    ArrayList<Integer> idManagerIds = IdManager.getIds();
    assertTrue("test successful adding of id", idManagerIds.contains(3));

  }
}
